/*
* @Author: Adolphus.Yao
* @Date:   2019-08-29 15:27:17
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-09-09 20:44:22
*/
package main

import (
    "fulnero.com/iris/mvc/datasource"
    "fulnero.com/iris/mvc/repositories"
    "fulnero.com/iris/mvc/services"
    "fulnero.com/iris/mvc/web/controllers"
    "fulnero.com/iris/mvc/web/middleware"

    "github.com/kataras/iris"
    "github.com/kataras/iris/mvc"
    "github.com/kataras/iris/sessions"
)

type Test struct {
    Name string
    Psw string
}

func main() {
    app := iris.New()
    app.Logger().SetLevel("debug")

    app.RegisterView(iris.HTML("./web/views", ".html"))
    /*app.Use(middleware.BasicAuth)
    repo := repositories.NewMovieRepository(datasource.Movies)
    movieService := services.NewMovieService(repo)
    mvc.New(app.Party("/movies")).Register(movieService).Handle(new(controllers.MovieController))*/
    mvc.Configure(app.Party("/movies"), movies)
    mvc.New(app.Party("/hello")).Handle(new(controllers.HelloController))

    app.Post("/test", func(ctx iris.Context){
        c := &Test{}

        if err := ctx.ReadJSON(c); err != nil{
            panic(err.Error())
        }else{
            ctx.JSON(c)
        }
    })

    app.Run(
        iris.Addr("localhost:8080"),
        //iris.WithoutVersionChecker,
        iris.WithoutServerError(iris.ErrServerClosed),
        iris.WithOptimizations,
        iris.WithoutBodyConsumptionOnUnmarshal,
    )
}

func movies(app *mvc.Application) {
    sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
    app.Router.Use(middleware.BasicAuth)
    repo := repositories.NewMovieRepository(datasource.Movies)
    movieService := services.NewMovieService(repo)
    app.Register(sess.Start, movieService)
    app.Handle(new(controllers.MovieController))
}
